import Layout from '@/Layout'

const generateRoutes = (item) => {
  const haveChildren = Array.isArray(item.children) && item.children.length > 0;
  const menuItem = {
    path: item.path,
    name: item.menuName,
    meta: {
      title: item.name
    }
  };
  // 区分主应用和子应用
  if (item.module == 'system') {
    if (item.type === "menu") {
      menuItem.component = (resolve) => {
        // 拼出相对路径，由于component无法识别变量
        require(["@/views" + item.path], resolve);
      }
    } else {
      menuItem.component = Layout
    }
  } else if (item.type == 'app') {
    menuItem.component = Layout
  }
  // menuItem.children = haveChildren ? item.children.map(i => mapTree(i)) : [],
  // return {
  //      id : item.gxqdm,
  //      text: item.text,
  //      children: haveChildren ? item.children.map(i => mapTree(i)) : [],
  //  }
  // return menuItem
  return {
    ...menuItem,
    children: haveChildren ? item.children.map(i => generateRoutes(i)) : [],
  }
};

export default generateRoutes;
